'use strict';

const Controller = require('egg').Controller;
const {getParam,postParam,renderTree} = require('../util/util.js');
class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }
  /**
   * methods:post,
   * @desc 登录接口
   * @param account {String} 账号
   * @param pwd  {String} 密码
  */
  async login() {
    const { ctx } = this;
    let account = getParam('account',ctx);
    let pwd = getParam('pwd',ctx);
    let info = await ctx.service.user.find(account);
    var resultData = {};
    resultData.status = 200;
    if(info){
      if(info.pwd.toString() === pwd.toString()){
        resultData.data = info;
        resultData.msg = "登录成功";
      }else{
        resultData.data = {};
        resultData.msg = "密码错误";
      }
    }else{
      resultData.data = {};
      resultData.msg = "账号不存在,请先注册";
    }
    ctx.body = resultData
  }
  /**
   * @desc 注册用户
   * @methods Post
   * @param accout 账号
   * @param pwd 密码
   * @param pid 父级id
  */
  async resignUser(){
    const { ctx } = this;
    let account = getParam('account',ctx);
    let pwd = getParam('pwd',ctx);
    let pid = getParam('pid',ctx) || 0;
    let result = await ctx.service.user.resign(account,pwd,pid);
    if (result.affectedRows === 1) {
      ctx.body = {
        status:200,
        msg:"注册成功",
        data:{},
      }
    } else {
      ctx.body = {
        status:200,
        msg:"注册失败",
        data:{},
      }
    }
  }
  /**
   * methods:post
   * @desc 查询所有用户
   * @param: 
  */ 
  async getAllUser(){
    const { ctx } = this;
    let allUser = await ctx.service.user.findAll();
    console.log(allUser)
    ctx.body = {
      status:200,
      data: renderTree(allUser),
      code:"获取成功"
    }
  }
  async getBanner(){
    const { ctx } = this;
    ctx.body = {
      status:200,
      msg:"获取成功",
      data:{
        list:[
          {url:"https://img.alicdn.com/tfs/TB1DAvisxD1gK0jSZFyXXciOVXa-800-800.jpg%20//img.alicdn.com/tfs/TB18X6fsrr1gK0jSZR0XXbP8XXa-800-800.png",id:1},
          {url:"https://img.alicdn.com/tfs/TB13MHesBv0gK0jSZKbXXbK2FXa-2000-2000.jpg%20//img.alicdn.com/tfs/TB1oK6fsCf2gK0jSZFPXXXsopXa-800-800.png",id:1},
        ]
      }
    }
  }
}
module.exports = HomeController;
